﻿using System;
using Sharp.DataTransferObject.CMS;
using Sharp.Domain.CMSBoundedContext.VIUModule.Aggregates;
using Sharp.Infrastructure;
using Sharp.Infrastructure.Search;

namespace Sharp.Application.CMS
{
    public partial class CMSServiceImpl
    {
        #region Special

        public Result DeleteSpecials(Guid[] ids)
        {
            var res = new Result();
            if (ids == null)
            {
                res.Err = "参数不可为空。";
                return res;
            }

            _specialRepository.Remove(ids);

            Context.Commit();

            return res;
        }

        public Result SaveSpecial(SpecialDTO dtoModel)
        {
            Result res = new Result();

            if (dtoModel.Id.IsEmpty())
            {
                var model = dtoModel.To<Special>();
                model.VIUs.AddRange(_viuRepository.GetList(x => dtoModel.VIUIds.Contains(x.Id)));

                _specialRepository.Add(model);
            }
            else
            {
                var persisted = _specialRepository.Get(dtoModel.Id);
                persisted.Title = dtoModel.Title;
                persisted.Content = dtoModel.Content;
                persisted.Thumbnail = dtoModel.Thumbnail;
                persisted.CoverPictrue = dtoModel.CoverPictrue;


                persisted.VIUs.Clear();
                persisted.VIUs.AddRange(_viuRepository.GetList(x => dtoModel.VIUIds.Contains(x.Id)));

                _specialRepository.Update(persisted);
            }
            _specialRepository.Context.Commit();
            return res;
        }

        public Result<SpecialDTO> GetSpecialInfo(Guid id)
        {
            var res = new Result<SpecialDTO>();
            res.Data = _specialRepository.Get(id, x => x.ApproveHistories, x => x.VIUs).To<SpecialDTO>();
            return res;
        }

        public PagedResult<SpecialDTO> GetSpecialPaged(PagingFeature pf)
        {
            var paged = _specialRepository.Pagging(pf);

            return paged.To<Special, SpecialDTO>();
        }

        #endregion
    }
}
